package com.yzx.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yzx.constants.Constants;
import com.yzx.vo.DataGridView;
import dto.SysMenuDto;
import dto.SysRoleDto;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yzx.mapper.SysRoleMapper;
import com.yzx.domain.SysRole;
import com.yzx.service.SysRoleService;
import org.springframework.util.unit.DataUnit;

@Service
public class SysRoleServiceImpl implements SysRoleService{
    @Autowired
    private SysRoleMapper sysRoleMapper;

    /**
     * 分页查询
     * @param roleDto
     * @return
     */
    @Override
    public DataGridView listRolePage(SysRoleDto roleDto) {
        Page<SysRole> page = new Page<>(roleDto.getPageNum(), roleDto.getPageSize());
        QueryWrapper<SysRole> wrapper = new QueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(roleDto.getRoleName()),SysRole.COL_ROLE_NAME,roleDto.getRoleName());
        wrapper.eq(StringUtils.isNotBlank(roleDto.getStatus()),SysRole.COL_STATUS,roleDto.getStatus());
        wrapper.like(StringUtils.isNotBlank(roleDto.getRoleCode()),SysRole.COL_ROLE_CODE,roleDto.getRoleCode());
        wrapper.ge(null!=roleDto.getBeginTime(),SysRole.COL_CREATE_TIME, roleDto.getBeginTime());
        wrapper.le(null!=roleDto.getEndTime(),SysRole.COL_CREATE_TIME,roleDto.getEndTime());
        wrapper.orderByAsc(SysRole.COL_ROLE_SORT);
        Page<SysRole> sysRolePage = this.sysRoleMapper.selectPage(page, wrapper);
        return new DataGridView(sysRolePage.getTotal(),sysRolePage.getRecords());
    }

    @Override
    public List<SysRole> listAllRoles() {
        QueryWrapper<SysRole> wrapper = new QueryWrapper<>();
        wrapper.eq(SysRole.COL_STATUS, Constants.STATUS_TRUE);
        return this.sysRoleMapper.selectList(wrapper);
    }

    @Override
    public SysRole getOne(Long roleId) {
        return this.sysRoleMapper.selectById(roleId);
    }

    @Override
    public int addRole(SysRoleDto roleDto) {
        SysRole sysRole = new SysRole();
        BeanUtil.copyProperties(roleDto,sysRole);
        sysRole.setCreateTime(DateUtil.date());
        sysRole.setCreateBy(roleDto.getSimpleUser().getUserName());
        return this.sysRoleMapper.insert(sysRole);
    }

    @Override
    public int updateRole(SysRoleDto roleDto) {
        SysRole sysRole = new SysRole();
        BeanUtil.copyProperties(roleDto,sysRole);
        sysRole.setUpdateTime(DateUtil.date());
        sysRole.setUpdateBy(roleDto.getSimpleUser().getUserName());
        return this.sysRoleMapper.updateById(sysRole);
    }

    @Override
    public int deleteRoleByIds(Long[] roleIds) {
        List<Long> longs = Arrays.asList(roleIds);
        if (null!=roleIds&&roleIds.length>0){
            this.sysRoleMapper.deleteRoleMenuByIds(longs);
            this.sysRoleMapper.deleteRoleUserByIds(longs);

            return this.sysRoleMapper.deleteBatchIds(longs);
        }
        return 0;
    }

    @Override
    public List<Long> queryMenuIdsByRoleId(Long roleId) {
        return this.sysRoleMapper.queryMenuIdsByRoleId(roleId);
    }

    @Override
    public void saveRoleMenu(Long roleId, Long[] menuId) {
        //删除所有原先权限
        this.sysRoleMapper.deleteRoleMenuByIds(Arrays.asList(roleId));
        if (menuId[0]!=-1){
            for (Long aLong : menuId) {
                this.sysRoleMapper.saveRoleMenu(roleId,aLong);
            }
        }
    }
}
